var translate = function(t){
			 t = t.split(" ")[0];	
			 t=t.replace("-","-");
			 t=t.replace("-","-");
			 return t;
		};

var LocString=String(window.document.location.href);
 function GetQueryString(str){
	 var rs=new RegExp("(^|)"+str+"=([^&]*)(&|$)","gi").exec(LocString),tmp;
	 if(tmp=rs)return tmp[2];
	 return "没有这个参数";
 }//获取url中的数据
var vedioSaveUrl = "http://114.116.41.171:8089/videos/";//服务器保存视频的路径
var pictureSaveUrl = "http://114.116.41.171:8089/pictures/";//服务器保存图片的路径
//var vedioSaveUrl = "http://192.168.3.67:8089/videos/";//服务器保存视频的路径
//var pictureSaveUrl = "http://192.168.3.67:8089/pictures/";//服务器保存图片的路径
/**
 * 根据广告id展示出广告主所要编辑广告的数据
 * 
 */
function editPictureAd(){
	var adId = GetQueryString("adId");
	$.ajax({
		url : '/web-adv/viewPlacementAd',
        data:{adId:adId},
		type : 'post',
		success : function(data) {
//			var resultVo = data.resultVo;
//			var picServerPrex = resultVo.picServerPrex;
//			var ads = data.bean;
//			$("#adName").html(ads.adName);	
//			$(".adCoverImg").attr("src",picServerPrex+ads.adHomepage);
			var beans = data.beans;
			var type = beans[0].deliveryCarrier;
			$("#adDescribeTimeBegin").text(translate(beans[0].starTime));
			$("#adDescribeTimeEnd").text(translate(beans[0].endTime));
			//时间框中时间显示
			$("#putStartTime").val(translate(beans[0].starTime));
			$("#putEndTime").val(translate(beans[0].endTime));
			
			$("#adId").val(beans[0].adId);
			$("#adName").text(beans[0].adName);
			//广告封面的路径
			$("#adImage").attr("src",pictureSaveUrl+beans[0].adHomepage);
			$("#setBalance").text(beans[0].releaseAmount);
			$("#setTotal").text(beans[0].deliveryRemainingAmount);
			$("#deliveryRemainingAmount").val(beans[0].deliveryRemainingAmount);
			$("#setPrice").text(beans[0].deliveryPrice);
			$("#typeImage").text(beans[0].deliveryCarrier);
			$("#adLogo").attr("src",pictureSaveUrl+beans[0].adHomepage);
			if(type=='pad'){
				$("#typeImg").prop("src","/web-adv/advWeb/static/images/padIcon.png");
		       }
		       if(type=='pc'){
		    	   $("#typeImg").prop("src","/web-adv/advWeb/static/images/pcIcon.png");
		       }
		       if(type=='mobile'){
		    	   $("#typeImg").prop("src","/web-adv/advWeb/static/images/mobileIcon.png");
		       }
		       if(type=='tv'){
		    	   $("#typeImg").prop("src","/web-adv/advWeb/static/images/tvIcon.png");
		       }
			for(var j = 0;j<beans.length;j++){
				if(beans[j].channelName!=null){
					$("#putCheckBox").attr("checked",false);
					var li = "<li>\
				     <img class='left' src='"+pictureSaveUrl+beans[j].logoUrl+"' alt=''>\
				     <input name='img' hidden class='left' value='"+beans[j].logoUrl+"' alt=''/>\
				     <button class='btn btn-primary left'>"+beans[j].channelName+"</button>\
				     <input name='plName' hidden value='"+beans[j].channelName+"'></input>\
				     <div class='right'>\
				     	 <p><input name='nameId' hidden value='"+beans[j].id+"'class='money' type='text' ></p>\
				     	 <p><input name='id' hidden value='"+beans[j].appId+"'class='money' type='text' ></p>\
			             <p><span>投放金额：</span><span>"+beans[j].channelAmount+"</span>(元)</p><input name='plTotlePrice' hidden value='"+beans[j].channelAmount+"'></input>\
			             <p><span>投放余额：</span><span>"+beans[j].advertiserBalance+"</span>(元)</p><input name='advertiserBalance' hidden value='"+beans[j].advertiserBalance+"'></input>\
			             <p><span>投放单价：</span><span>"+beans[j].channelPrice+"</span>(次/元)</p><input name='plPrice' hidden value='"+beans[j].channelPrice+"'></input>\
			         </div>\
					 <div class='clear'></div>\
				 </li>"
					$("#placement").append(li);
				}else{
					var li = "<p><input name='nameId' hidden value='"+beans[j].id+"'class='money' type='text' ></p>\
					     	 <p><input name='id' value='"+beans[j].appId+"'class='money' type='text' hidden ></p>\
					     	 <p><input name='plTotlePrice' hidden value='"+beans[j].channelAmount+"'></input>"
						$("#placement").append(li);
				}
			}
			//广告属性标签
			var classfyIds = beans[0].adClassify.split("|");
			for(var i =0;i<classfyIds.length;i++){
				if(classfyIds[i].length != 0){
				//	console.log(classfyIds[i]);
					var li = "<li><img src=\"/web-adv/advWeb/static/images/genres_pic"+classfyIds[i]+".png\" alt=\"\" /></li>";
					$("#adClassify").append(li);
				}
			}
		},
        error: function(){}        
	});
}

/**
 * 查出所有渠道
 * 
 */
$("#getPlacements").click(function () {
	selectAdDitch();
	var optionsDev="null";
	//总金额
	var releaseAmount = $("#setBalance").text();
	var deliveryPrice = $("#setPrice").text();
	//数据库中的余额
	var deliveryRemainingAmount = $("#deliveryRemainingAmount").val();
	//现在的余额
	var deliveryAmount = $("#setTotal").text();
	var channelAmount = 0.0;
	//现
	var releaseamount = releaseAmount*1+deliveryAmount*1-deliveryRemainingAmount*1;
	var dPrice = $("input[name='plPrice']");
	//账户余额
	var adMoney = $("#actualBalance").html();
	var UsMoney = adMoney-deliveryAmount*1+deliveryRemainingAmount*1;
	if(UsMoney<0){
		alert("您的账户余额不足");
		return false;
	}
	 var beans = $("input[name='plTotlePrice']");
		 for(var j = 0; j < beans.length;j++){
			 channelAmount += beans[j].value*1;
			 var amount = channelAmount-beans[j].value*1;
			 var money = releaseamount*1-amount;
			 if(beans[j].value==""||beans[j].value==null){
				 alert("请输入投放金额");
				 return false;
			 }
			 if(dPrice[j]!=undefined){
			 var moneys = beans[j].value-dPrice[j].value;
				if(moneys<0){
					alert("渠道总金额不能小于单价");
					dPrice[j].value = "";
					return false;
				}
			 }
			 if(parseInt(channelAmount)>parseInt(releaseamount)){
				 alert("您还有"+money+"元可用");
				 beans[j].value="";
				 return false;
			 }
			 if(channelAmount==releaseamount){
				 alert("您还有0元可用");
				 return false;
			 }
		 }
	var placement = $("input[name='id']");
	var ajax = $.ajax({  
        async : false,  
        cache:false,  
        type: 'POST',  
        dataType : "json",  
        url: '/web-adv/finishApprovider',  //请求的路径
        success:function(data){//controller层传过来的map 
//        	var placement = $("input[name='id']");
        	var beans = data.beans;
        	//选过之后的不能再选(渠道)
        	for(var i = 0; i < placement.length;i++){
       			for(var j = 0; j < beans.length;j++){
       				if(placement[i].value==beans[j].id){
       					beans.splice([j],1);
       				}
       			}
   		 	}
        	var va = placement[0].value;
        	if(placement.length==1&&va=='null'){
    			alert("您已经选择了全渠道"); 
	    	}else{
	    		for(var j = 0; j < beans.length;j++){
	        		optionsDev+="<option value=\'"+j+"' data-adName=\'"+beans[j].name+"' data-img=\'"+beans[j].logoUrl+"' data-id=\'"+beans[j].id+"'\>"+beans[j].name+"</option>";
	        	}
	    	}
         }   
    });
   $("#advApp").html(optionsDev);
});
/**
 * 编辑图片广告
 * 
 */
$("#editAd").click(function (){
	//时间
	var planStart = $("#adDescribeTimeBegin").text();
	var planEnd = $("#adDescribeTimeEnd").text();
	//广告id
	var adId = $("#adId").val();
	//广告总金额
	var releaseAmount = $("#setBalance").text();
	//广告单价
	var deliveryPrice = $("#setPrice").text();
	//广告剩余金额
	var deliveryRemainingAmount = $("#setTotal").text();
	//数据库中的余额
	var deliveryRemainingAmounts = $("#deliveryRemainingAmount").val();
	//现在的余额
	var deliveryAmount = $("#setTotal").text();
	//现
	var releaseamount = releaseAmount*1+deliveryAmount*1-deliveryRemainingAmounts*1;
	//判断渠道单价
	 var dPrice = $("input[name='plPrice']");
	  for(var j = 0; j < dPrice.length;j++){
			 if(dPrice[j].value==""||dPrice[j].value==null){
				 dPrice[j].value = deliveryPrice;
			 }
		 }
	//渠道名称
	  var channelName="";
		$("input[name='plName']").each(
				 function(){
					 if($(this).val()!=""||$(this).val()!=null){
						 channelName += $(this).val()+','
					}
				 } 
		 )
		 //渠道总金额
		 var channelAmount="";
		 $("input[name='plTotlePrice']").each(  
				 function(){  
					 channelAmount += $(this).val()+',';
				 } 
		 )
		 //渠道单价
	var channelPrice="";
		 $("input[name='plPrice']").each(  
				 function(){  
					 channelPrice += $(this).val()+',';
				 } 
		 )
		 //渠道余额
		 var advertiserBalance="";
		 $("input[name='advertiserBalance']").each(  
				 function(){  
					 advertiserBalance += $(this).val()+',';
				 } 
		 )
		 //渠道图片路径
	 var logoUrl="";
	 $("input[name='img']").each(  
			 function(){  
				 logoUrl += $(this).val()+',';
			 } 
	 )
	 //渠道对应的外键id(名称)
	 var appId = "";
	 $("input[name='id']").each(  
			 function(){ 
				 appId += $(this).val()+',';
			 } 
	 )
	 //渠道id
	 var id = "";
	 $("input[name='nameId']").each(  
			 function(){ 
				 if($(this).val()==""||$(this).val()==null){
					 id += '1,'; 
				 }else{
					 id += $(this).val()+',';
				 }
			 } 
	 )
	 var param = {
				"planStart" : planStart,
				"planEnd" : planEnd,
				"adId" : adId,
				"releaseAmount" : releaseAmount,
				"deliveryPrice" : deliveryPrice,
				"deliveryRemainingAmount" : deliveryRemainingAmount,
				"channelName" : channelName,
				"channelAmount" : channelAmount,
				"advertiserBalance" : advertiserBalance,
				"channelPrice" : channelPrice,
				"logoUrl" : logoUrl,
				"appId" : appId,
				"id" : id
			}
	 if(planStart==""||planStart==null||planEnd==""||planEnd==null){
		 alert("日期输入有误不能为空");
		 return false;
	 }
	 if(planStart>planEnd){
		 alert("日期输入有误，请让起始时间小于截止时间");
		 return false;
	 }
	 var channelAmount = 0.0;
	 var beans = $("input[name='plTotlePrice']");
		for(var j = 0; j < beans.length;j++){
			 channelAmount += beans[j].value*1;
			 var amount = channelAmount-beans[j].value*1;
			 var money = releaseamount*1-amount;
			 if(dPrice[j]!=undefined){
				 var moneys = beans[j].value-dPrice[j].value;
					if(moneys<0){
						alert("渠道总金额不能小于单价");
						dPrice[j].value = "";
						return false;
					}
				 }
			 if(parseInt(channelAmount)>parseInt(releaseamount)){
				 alert("您还有"+money+"元可用");
				 beans[j].value="";
				 return false;
			 }
			 if(beans[j].value==""||beans[j].value==null){
				 alert("渠道总金额不能为空");
				 return false;
			 }
		 }
	//账户余额
		var adMoney = $("#actualBalance").html();
		var UsMoney = adMoney-deliveryAmount*1+deliveryRemainingAmounts*1;
		if(UsMoney<0){
			alert("您的账户余额不足");
			return false;
		}
		if(deliveryPrice==0||deliveryPrice==""){
			 alert("请输入正确的单价");
			 return false;
		 }
		if(parseInt(releaseamount)<parseInt(deliveryPrice)){
			 alert("广告总额不能小于单价");
//			 $("#setPrice").text("");
			 return false;
		 }
		$("#throwButt").prop("disabled","disabled");
	$.ajax({
		url : '/web-adv/editPicturePlacementAd',
        data:param,
		type : 'post',
		success : function(data) {
			var check = data.check;
			alert(check);
		location.href ="/web-adv/advWeb/views/adManagement/alreadyAd/index.html?selectedNav%3D2%26navPos%3D0";
		},
        error: function(){}        
	});
})








//222222222222
function selectAdDitch(){
    //var dom = ele.parent("div.form-group").siblings("select.selectAdModalOption");
	 var dom=$("#advApp");
	 if(dom.val()){	
        var valDom = dom.children("option").eq(dom.val()),name=valDom.attr("data-adName"),
		 price = valDom.attr("data-price"),total = valDom.attr("data-total"),img = valDom.attr("data-img"),id = valDom.attr("data-id");						 
		 var itemText = '<p><img src="'+pictureSaveUrl+img+'">'+ 
		 '<input name="plName" value="'+name+'" class="advertisement" type="button"><input name="id" value="'+id+'" class="advertisement" type="button" hidden><input name="img" value="'+img+'" class="advertisement" type="button" hidden> 投放总金额 <input name="plTotlePrice" value="" class="money" type="text">'+ 
		 '<b>(元)</b>单价<input name="plPrice" class="price" value="" type="text"><b>(次/元)</b>'+ 
		 '<input value="x&nbsp;删除" class="delete" type="button"></p>';
		 $("#channelSelect").append(itemText);
		 $("#selectAdDitch").next().trigger("click");
	 }else{
		 
	 }				 					 				     
};